/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * RSTransformationElement.cpp
 *
 *  Created on: 2010/03/04
 *      Author: Alexandre Pires ( c.alexandre.pires@gmail.com )
 */

#include "RSTransformationElement.h"

namespace Realsphere {

    RSTransformationElement::RSTransformationElement ( RSTransformationType rTransformationType, const QString& rScopeId ) :
                RSElement ( RSTransformationElement::TransformationTypeToTag ( rTransformationType ) )
    {
        mScopeId = rScopeId;
    }

    RSTransformationElement::~RSTransformationElement ()
    {
    }

    QString RSTransformationElement::ScopeId () const
    {
        return mScopeId;
    }

    QMatrix4x4 RSTransformationElement::Transformation ()
    {
        if ( mDirty )
            UpdateTransformation ();

        return mTransformation;
    }

    RSTransformationType RSTransformationElement::TransformationType () const
    {
        return mTransformationType;
    }

    bool RSTransformationElement::IsDirty () const
    {
        return mDirty;
    }

    bool RSTransformationElement::IsRootElement () const
    {
        return false;
    }

    void RSTransformationElement::SetScopeId ( const QString& rScopeId, bool rEmit )
    {
        mScopeId = rScopeId;

        if ( rEmit )
            emit ScopeIdChanged ( mScopeId );
    }

    void RSTransformationElement::SetDirty ( bool rOnOff )
    {
        mDirty = rOnOff;
    }

    RSError RSTransformationElement::BeforeAddChildElement ( RSElement* rChild )
    {
        return RS_NO_CHILD_ALLOWED;
    }

    void RSTransformationElement::SetTransformation ( const QMatrix4x4& rTransformation )
    {
        mTransformation = rTransformation;
    }

    RSTransformationType RSTransformationElement::TransformationTagtoType ( const QString& rTag )
    {
        if ( rTag == RS_LOOK_AT_TAG )
        {
            return RS_LOOK_AT;
        }
        else if ( rTag == RS_MATRIX_TAG )
        {
            return RS_MATRIX;
        }
        else if ( rTag == RS_ROTATE_TAG )
        {
            return RS_ROTATE;
        }
        else if ( rTag == RS_SCALE_TAG )
        {
            return RS_SCALE;
        }
        else if ( rTag == RS_SKEW_TAG )
        {
            return RS_SKEW;
        }
        else if ( rTag == RS_TRANSLATE_TAG )
        {
            return RS_TRANSLATE;
        }

        return RS_TRANSFORMATION_UNKNOWN;
    }

    QString RSTransformationElement::TransformationTypeToTag ( RSTransformationType rTransformationType )
    {
        switch ( rTransformationType )
        {
            case RS_LOOK_AT:
                return QString ( RS_LOOK_AT_TAG );
                break;
            case RS_MATRIX:
                return QString ( RS_MATRIX_TAG );
                break;
            case RS_ROTATE:
                return QString ( RS_ROTATE_TAG );
                break;
            case RS_SCALE:
                return QString ( RS_SCALE_TAG );
                break;
            case RS_SKEW:
                return QString ( RS_SKEW_TAG );
                break;
            case RS_TRANSLATE:
                return QString ( RS_TRANSLATE_TAG );
                break;
            default:
                return QString ( RS_ELEMENT_TAG );
                break;
        }
    }
}
